#
# Copyright (C) Since 2018 ~ 2025, Inc. All Rights Reserved
#
# @Time    : 2025/1/9-下午7:08
# @Author  : Timmy
# @Email   : tangping@cloudstream-tech.com
# @File    : protocol.py
# @Software: lottery
# @Function:
#
import json

from models.types import Award, People
from .websocket_client import Connection


class Terminal(Connection):
    def __init__(self):
        super().__init__()

    async def sync_namelist(self, people: list[People]) -> None:
        content = {
            "action": "syncNameList",
            "data": {
                "people": [p.json() for p in people]
            }
        }

        msg = json.dumps(content, sort_keys=True)
        await self.send_message(msg)

    async def sync_current_award(self, award: Award, each: int) -> None:
        content = {
            "action": "syncCurrentAward",
            "data": {
                "title": award.name,
                "total": award.count,
                "eachNum": each
            }
        }

        msg = json.dumps(content, sort_keys=True)
        await self.send_message(msg)

    async def start_roll(self):
        content = {"action": "startRoll", "data": {}}
        msg = json.dumps(content)
        await self.send_message(msg)

    async def stop_roll(self):
        content = {"action": "stopRoll", "data": {}}
        msg = json.dumps(content)
        await self.send_message(msg)

    async def show_shot(self, people: list[People]):
        content = {"action": "shot", "data": {
            "people": [p.json() for p in people]
        }}
        msg = json.dumps(content, sort_keys=True)
        await self.send_message(msg)

    async def show_whole_lucky(self, awards: list[Award]):
        content = {"action": "showWholeLucky", "data": {
            "title": "中奖人员名单",
            "items": [
                {
                    "title": a.name,
                    "people": [
                        {"name": p.name, "dep": p.dep, "icon": None}
                        for p in a.lucky
                    ]
                } for a in awards
            ],
        }}

        msg = json.dumps(content, sort_keys=True)
        await self.send_message(msg)

    async def show_award_lucky(self, item: Award, lucky: list[People]):
        content = {"action": "showAwardLucky", "data": {
            "title": item.name,
            "people": [p.json() for p in lucky],
        }}
        msg = json.dumps(content, sort_keys=True)
        await self.send_message(msg)

    async def show_namelist(self):
        content = {"action": "showNameList", "data": {}}
        msg = json.dumps(content)
        await self.send_message(msg)

    async def main_page(self):
        content = {"action": "mainPage", "data": {}}
        msg = json.dumps(content)
        await self.send_message(msg)